// Polyfill from https://github.com/anonyco/FastestSmallestTextEncoderDecoder
(function(global) {
  'use strict';

  if (global.TextDecoder) {
    return;
  }

  function TextDecoder() {}

  TextDecoder.prototype.decode = function(octets) {
    var string = "";
    var i = 0;
    while (i < octets.length) {
      var octet = octets[i];
      var bytesNeeded = 0;
      var codePoint = 0;
      if (octet <= 0x7F) {
        bytesNeeded = 0;
        codePoint = octet & 0xFF;
      } else if (octet <= 0xDF) {
        bytesNeeded = 1;
        codePoint = octet & 0x1F;
      } else if (octet <= 0xEF) {
        bytesNeeded = 2;
        codePoint = octet & 0x0F;
      } else if (octet <= 0xF4) {
        bytesNeeded = 3;
        codePoint = octet & 0x07;
      }
      if (octets.length - i - bytesNeeded > 0) {
        var k = 0;
        while (k < bytesNeeded) {
          octet = octets[i + k + 1];
          codePoint = (codePoint << 6) | (octet & 0x3F);
          k += 1;
        }
      }
      if (codePoint <= 0xFFFF) {
        string += String.fromCharCode(codePoint);
      } else {
        codePoint -= 0x10000;
        string += String.fromCharCode(
          (codePoint >> 10) & 0x3FF | 0xD800,
          codePoint & 0x3FF | 0xDC00
        );
      }
      i += bytesNeeded + 1;
    }
    return string;
  };

  global.TextDecoder = TextDecoder;
})((typeof globalThis !== 'undefined' && globalThis) || (typeof self !== 'undefined' && self) || (typeof window !== 'undefined' && window) || (typeof global !== 'undefined' && global) || this || {});